$(function () {
    $("#stotage_datagrid").datagrid({
        fitColumns: true,
        fit: true,
        toolbar: '#stotage_toolbar',
        url: '/guard/storageList.do',
        pagination: true,
        striped: true,
        rownumbers: true,
        singleSelect: true,
        columns: [[
            {title: '保单编号', field: 'num', align: 'center', width: '100'},
            {
                title: '投保人', field: 'client', align: 'center', width: '100', formatter: function (value, row, index) {
                    return row ? row.client.name : "";
                }
            },
            {title: '申请日期', field: 'appDate', align: 'center', width: '100'},
            {title: '核保日期', field: 'auditDate', align: 'center', width: '100'},
            {title: '保险截止日', field: 'overtime', align: 'center', width: '100'},
            {
                title: '保险时长',
                field: 'product.years',
                align: 'center',
                width: '100',
                formatter: function (value, row, index) {
                    return row ? row.product.years : "";
                }
            },
            {title: '业务员', field: 'employeeName', align: 'center', width: '100'},
            {title: '审核人', field: 'auditName', align: 'center', width: '100'},
            {
                title: '投保总金额',
                field: 'product.cost',
                align: 'center',
                width: '100',
                formatter: function (value, row, index) {
                    return row ? row.product.cost : "";
                }
            },
            {
                title: '状态', field: 'state', align: 'center', width: '100', formatter: function (value, row, index) {
                    return "<font color='red'>待提交</font>"
                }
            }

        ]]

    })

    $("#stotage_dialog").dialog({
        width: 350,
        height: 470,
        buttons: '#stotage_buttons',
        closed: true

    })

    var methodObj = {
        //删除按钮
        deleted: function () {
            var row = $("#stotage_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择要操作的数据", "info");
                return;
            }
            $.messager.confirm("温馨提示", "你确定要删除吗？", function (r) {
                if (r) {
                    $.get("/guard/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", "操作成功", "info", function () {
                                $("#stotage_datagrid").datagrid("reload");
                            })
                        } else {
                            $.messager.alert('温馨提示', data.msg);
                        }
                    });
                }
            })

        },
        //提审按钮
        pending: function () {
            var row = $("#stotage_datagrid").datagrid("getSelected");
            var text = $("#btn_state").text();
            if (!row) {
                $.messager.alert("温馨提示", "请选择要操作的数据", "info");
                return;
            }
            $.messager.confirm("温馨提示", "你确定要提交审核吗？", function (r) {
                if (r) {
                    $.get("/guard/toPending.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", "提交成功", "info", function () {
                                $("#stotage_datagrid").datagrid("reload");
                            })
                        } else {
                            $.messager.alert('温馨提示', data.msg);
                        }
                    });
                }
            })

        },

        //刷新按钮
        reload: function () {
            $("#stotage_datagrid").datagrid("reload");
        },
        query: function () {
            var clientName = $("#clientName").textbox("getValue");
            var num = $("#num").textbox("getValue");

            $("#stotage_datagrid").datagrid('load', {
                clientName: clientName,
                num: num
            });
        },
        //取消按钮
        cancel: function () {
            $("#stotage_dialog").dialog("close");
        },
        //查看明细
        lookDetail: function () {
            var row = $("#stotage_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择要查看的行", "info");
                return;
            }
            console.log(row);
            if (row.product){
                    row['product.name'] = row.product.name
                }
            if (row.product){
                    row['product.quota'] = row.product.quota
                }
            if (row.car){
                    row['car.number'] = row.car.number
                }
            if (row.car){
                    row['car.name'] = row.car.name
                }
            if (row.product.promise){
                    row['product.promise.name'] = row.product.promise.name
                }
                $("#stotage_form").form('load',row);


            $("#stotage_dialog").dialog('open')


        }

    }

    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd](this);
    })

    $("#stotage_dialog").dialog({
        width: 350,
        height: 470,
        buttons: '#emp_buttons',
        closed: true,
        title:"明细"

    })


})

